package com.qcq.springcloud.controller;

import com.qcq.springcloud.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * @author ：qcq
 * @date ：Created in 2020/11/26 20:39
 * @description：
 */
@RestController
@RequestMapping("consumer")
public class ConsumerController {



//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

/*    @GetMapping("{username}")
    public Map queryByUsername(@PathVariable("username") String username) {

//        String url = "http://localhost:8081/user/"+username;
        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        String uri = serviceInstance.getUri().toString();
        String url = uri+"/user/"+username;
        Map userMap = restTemplate.getForObject(url, Map.class);
        System.out.println(userMap);
        return userMap;
    }*/


//    @GetMapping("{username}")
//    public User queryByUsername(@PathVariable("username") String username) {
//        String url = "http://user-service/user/" + username; // user_service 微服务名称
//        User user = restTemplate.getForObject(url, User.class);
//
//        return user;
//    }

}
